package com.guofen.controller.sjq;


import com.guofen.pojo.SjqWaterProductionDeliverWater;
import com.guofen.pojo.SjqWeightProductionFaeces;
import com.guofen.pojo.SjqWeightProductionKitchen;
import com.guofen.service.sjq.SjqWeightProductionService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequestMapping("/sjq/weightProduction")
@Controller
@Api(tags = "SjqWeightProductionController", description = "四季青生产现场称重")
public class SjqWeightProductionController {
    @Autowired
    private SjqWeightProductionService sjqWeightProductionService;

    @ApiOperation("添加粪便称重记录")
    @RequestMapping(value = "/addFormFaeces", method = RequestMethod.POST)
    @ResponseBody
    public Result addFormFaeces(@ApiParam(name = "editTextDate", value = "日期", required = true) @RequestParam String editTextDate,
                                @ApiParam(name = "editTextStartTime", value = "开机时间", required = true) @RequestParam String editTextStartTime,
                                @ApiParam(name = "editTextCloseTime", value = "关机时间", required = true) @RequestParam String editTextCloseTime,
                                @ApiParam(name = "editTextChengLiang", value = "称量软件是否正常", required = true) @RequestParam String editTextChengLiang,
                                @ApiParam(name = "editTextJianKong", value = "监控图像是否正常", required = true) @RequestParam String editTextJianKong,
                                @ApiParam(name = "editTextShuJu", value = "数据同步是否正常", required = true) @RequestParam String editTextShuJu,
                                @ApiParam(name = "editTextYuanCheng", value = "远程连接是否正常", required = true) @RequestParam String editTextYuanCheng,
                                @ApiParam(name = "hd_checi", value = "海淀环卫车次", required = true) @RequestParam String hd_checi,
                                @ApiParam(name = "hd_zhongliang", value = "海淀环卫重量", required = true) @RequestParam String hd_zhongliang,
                                @ApiParam(name = "sq_checi", value = "四清分公司车次", required = true) @RequestParam String sq_checi,
                                @ApiParam(name = "sq_zhongliang", value = "四清分公司重量", required = true) @RequestParam String sq_zhongliang,
                                @ApiParam(name = "bs_checi", value = "宝山车次", required = true) @RequestParam String bs_checi,
                                @ApiParam(name = "bs_zhongliang", value = "宝山重量", required = true) @RequestParam String bs_zhongliang,
                                @ApiParam(name = "tf_checi", value = "田丰车次", required = true) @RequestParam String tf_checi,
                                @ApiParam(name = "tf_zhongliang", value = "田丰重量", required = true) @RequestParam String tf_zhongliang,
                                @ApiParam(name = "jz_checi", value = "进站粪便合计车次", required = true) @RequestParam String jz_checi,
                                @ApiParam(name = "jz_zhongliang", value = "进站粪便合计重量", required = true) @RequestParam String jz_zhongliang,
                                @ApiParam(name = "gy_checi", value = "固液分离残渣（五路居）车次", required = true) @RequestParam String gy_checi,
                                @ApiParam(name = "gy_zhongliang", value = "固液分离残渣（五路居）重量", required = true) @RequestParam String gy_zhongliang,
                                @ApiParam(name = "operator", value = "操作人", required = true) @RequestParam String operator,
                                @ApiParam(name = "additon", value = "备注", required = false) @RequestParam String additon
    ){
        int tr1 = Integer.parseInt(hd_checi);
        Double w1 = Double.parseDouble(hd_zhongliang);
        int tr2 = Integer.parseInt(sq_checi);
        Double w2 = Double.parseDouble(sq_zhongliang);
        int tr3 = Integer.parseInt(bs_checi);
        Double w3 = Double.parseDouble(bs_zhongliang);
        int tr4 = Integer.parseInt(tf_checi);
        Double w4 = Double.parseDouble(tf_zhongliang);
        int ttotal = Integer.parseInt(jz_checi);
        Double wtotal = Double.parseDouble(jz_zhongliang);
        int tre = Integer.parseInt(gy_checi);
        Double wre = Double.parseDouble(gy_zhongliang);

        if(sjqWeightProductionService.addFormFaeces(editTextDate,editTextStartTime,editTextCloseTime,editTextChengLiang,editTextJianKong,editTextShuJu,
                editTextYuanCheng,tr1,w1,tr2,w2,tr3,w3,tr4,w4,ttotal,wtotal,tre,wre,operator,additon)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("保存失败");
        }
    }


    @ApiOperation("添加厨余垃圾称重记录")
    @RequestMapping(value = "/addFormKitchen", method = RequestMethod.POST)
    @ResponseBody
    public Result addFormKitchen(@ApiParam(name = "editTextDate", value = "日期", required = true) @RequestParam String editTextDate,
                                 @ApiParam(name = "editTextStartTime", value = "开机时间", required = true) @RequestParam String editTextStartTime,
                                 @ApiParam(name = "editTextCloseTime", value = "关机时间", required = true) @RequestParam String editTextCloseTime,
                                 @ApiParam(name = "editTextChengLiang", value = "称量软件是否正常", required = true) @RequestParam String editTextChengLiang,
                                 @ApiParam(name = "editTextJianKong", value = "监控图像是否正常", required = true) @RequestParam String editTextJianKong,
                                 @ApiParam(name = "editTextShuJu", value = "数据同步是否正常", required = true) @RequestParam String editTextShuJu,
                                 @ApiParam(name = "editTextYuanCheng", value = "远程连接是否正常", required = true) @RequestParam String editTextYuanCheng,
                                @ApiParam(name = "hd5_checi", value = "海淀环卫五队车次", required = true) @RequestParam String hd5_checi,
                                @ApiParam(name = "hd5_zhongliang", value = "海淀环卫五队重量", required = true) @RequestParam String hd5_zhongliang,
                                @ApiParam(name = "hd8_checi", value = "海淀环卫八队车次", required = true) @RequestParam String hd8_checi,
                                @ApiParam(name = "hd8_zhongliang", value = "海淀环卫八队重量", required = true) @RequestParam String hd8_zhongliang,
                                @ApiParam(name = "jz_checi", value = "进站厨余垃圾合计车次", required = true) @RequestParam String jz_checi,
                                @ApiParam(name = "jz_zhongliang", value = "进站厨余垃圾合计重量", required = true) @RequestParam String jz_zhongliang,
                                @ApiParam(name = "operator", value = "操作人", required = true) @RequestParam String operator,
                                @ApiParam(name = "additon", value = "备注", required = false) @RequestParam String additon
    ){
        int tr1 = Integer.parseInt(hd5_checi);
        Double w1 = Double.parseDouble(hd5_zhongliang);
        int tr2 = Integer.parseInt(hd8_checi);
        Double w2 = Double.parseDouble(hd8_zhongliang);
        int ttotal = Integer.parseInt(jz_checi);
        Double wtotal = Double.parseDouble(jz_zhongliang);

        if(sjqWeightProductionService.addFormKitchen(editTextDate,editTextStartTime,editTextCloseTime,editTextChengLiang,editTextJianKong,editTextShuJu,editTextYuanCheng,
        tr1,w1,tr2,w2,ttotal,wtotal,operator,additon)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("保存失败");
        }
    }

    @ApiOperation("查询粪便记录")
    @RequestMapping(value = "/findFormFaeces", method = RequestMethod.GET)
    @ResponseBody
    public Result findFormFaeces(){
        if (sjqWeightProductionService.findFormFaeces() != null)
            return ResultGenerator.genSuccessResult(sjqWeightProductionService.findFormFaeces());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }


    @ApiOperation("查询厨余垃圾记录")
    @RequestMapping(value = "/findFormKitchen", method = RequestMethod.GET)
    @ResponseBody
    public Result findFormKitchen(){
        if (sjqWeightProductionService.findFormKitchen() != null)
            return ResultGenerator.genSuccessResult(sjqWeightProductionService.findFormKitchen());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }

    @ApiOperation("EXCEL批量添加记录")
    @PostMapping("/addFromKitchenExcel")
    @ResponseBody
    public Result doRegisterFromExcel(@RequestParam("file") MultipartFile file){
        String EXCEL_XLS = "xls";
        String EXCEL_XLSX = "xlsx";

        if (file.isEmpty())
            return ResultGenerator.genFailResult("文件为空");
        System.out.println(file.getOriginalFilename());
        List<Boolean> registerResult = new ArrayList<>();

        try {
            //根据路径获取这个操作excel的实例
            Sheet sheet = null;
            Row row = null;
            if (file.getOriginalFilename().endsWith(EXCEL_XLS)) {
                HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
                sheet = wb.getSheetAt(0);
            } else if (file.getOriginalFilename().endsWith(EXCEL_XLSX)) {
                XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
                //根据页面index 获取sheet页
                sheet = wb.getSheetAt(0);
            }

            List<SjqWeightProductionKitchen> sjqWeightProductionKitchens = new ArrayList<>();

            for (int i = 2; i < Objects.requireNonNull(sheet).getPhysicalNumberOfRows(); i = i + 6) {
                //获取每一行数据
                row = sheet.getRow(i);
                if(row.getCell(0)==null && row.getCell(1)==null && row.getCell(2)==null && row.getCell(3)==null
                        && row.getCell(4)==null && row.getCell(5)==null && row.getCell(6)==null
                        && row.getCell(7)==null && row.getCell(8)==null){
                    registerResult.add(false);
                    continue;
                }
                SjqWeightProductionKitchen sjqWeightProductionKitchen = new SjqWeightProductionKitchen();
                if(row.getCell(0)!=null){
                    Cell cell = row.getCell(0);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setDate(cell.getStringCellValue());
                }
                if(row.getCell(1)!=null){
                    Cell cell = row.getCell(1);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setStartTime(cell.getStringCellValue());
                }
                if(row.getCell(2)!=null){
                    Cell cell = row.getCell(2);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setCloseTime(cell.getStringCellValue());
                }
                if(row.getCell(3)!=null){
                    Cell cell = row.getCell(3);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setSoftNormal(cell.getStringCellValue());
                }
                if(row.getCell(4)!=null){
                    Cell cell = row.getCell(4);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setMonitorNormal(cell.getStringCellValue());
                }
                if(row.getCell(5)!=null){
                    Cell cell = row.getCell(5);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setDataNormal(cell.getStringCellValue());
                }
                if(row.getCell(6)!=null){
                    Cell cell = row.getCell(6);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setConnectNormal(cell.getStringCellValue());
                }
                if(row.getCell(7)!=null){
                    Cell cell = row.getCell(7);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setOperator(cell.getStringCellValue());
                }
                if(row.getCell(8)!=null){
                    Cell cell = row.getCell(8);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionKitchen.setNote(cell.getStringCellValue());
                }

                //环卫五队
                Row row1 = sheet.getRow(i + 2);
                if(row1.getCell(4)==null && row1.getCell(5)==null || row1.getCell(4).getNumericCellValue() == 0
                || row1.getCell(5).getNumericCellValue() == 0){
                    registerResult.add(false);
                    continue;
                }
                if(row1.getCell(4)!=null){
                    sjqWeightProductionKitchen.setTrain1((int)row1.getCell(4).getNumericCellValue());
                }
                if(row1.getCell(5)!=null){
                    sjqWeightProductionKitchen.setWeight1(row1.getCell(5).getNumericCellValue());
                }

                //环卫八队
                Row row2 = sheet.getRow(i + 3);
                if(row2.getCell(4) == null && row2.getCell(5) == null || row2.getCell(4).getNumericCellValue() == 0
                 || row2.getCell(5).getNumericCellValue() == 0){
                    registerResult.add(false);
                    continue;
                }
                if(row2.getCell(4) != null){
                    sjqWeightProductionKitchen.setTrain2((int)row2.getCell(4).getNumericCellValue());
                }
                if(row2.getCell(5) != null){
                    sjqWeightProductionKitchen.setWeight2(row2.getCell(5).getNumericCellValue());
                }

                //合计
                Row row3 = sheet.getRow(i + 4);
                if(row3.getCell(4) == null && row3.getCell(5)==null || row3.getCell(4).getNumericCellValue() == 0
                        || row3.getCell(5).getNumericCellValue() == 0){
                    registerResult.add(false);
                    continue;
                }
                if(row3.getCell(4)!=null){
                    sjqWeightProductionKitchen.setTrainTotal((int)row3.getCell(4).getNumericCellValue());
                }
                if(row3.getCell(5)!=null){
                    sjqWeightProductionKitchen.setWeightTotal(row3.getCell(5).getNumericCellValue());
                }
                sjqWeightProductionKitchens.add(sjqWeightProductionKitchen);
            }
            for (SjqWeightProductionKitchen u : sjqWeightProductionKitchens) {
                Boolean test;
                registerResult.add(test = sjqWeightProductionService.addFromKitchenExcel(u.getDate(), u.getStartTime()
                        , u.getCloseTime(), u.getSoftNormal(), u.getMonitorNormal(), u.getDataNormal(), u.getConnectNormal()
                , u.getOperator(), u.getNote(), u.getTrain1(), u.getWeight1(), u.getTrain2(), u.getWeight2(), u.getTrainTotal()
                , u.getWeightTotal()));
                System.out.println(test);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult(registerResult);
    }

    @ApiOperation("EXCEL批量添加记录")
    @PostMapping("/addFromFaecesExcel")
    @ResponseBody
    public Result doRegisterFromExcel1(@RequestParam("file") MultipartFile file){
        String EXCEL_XLS = "xls";
        String EXCEL_XLSX = "xlsx";

        if (file.isEmpty())
            return ResultGenerator.genFailResult("文件为空");
        System.out.println(file.getOriginalFilename());
        List<Boolean> registerResult = new ArrayList<>();

        try {
            //根据路径获取这个操作excel的实例
            Sheet sheet = null;
            Row row = null;
            if (file.getOriginalFilename().endsWith(EXCEL_XLS)) {
                HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
                sheet = wb.getSheetAt(0);
            } else if (file.getOriginalFilename().endsWith(EXCEL_XLSX)) {
                XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
                //根据页面index 获取sheet页
                sheet = wb.getSheetAt(0);
            }

            List<SjqWeightProductionFaeces> sjqWeightProductionFaeces1= new ArrayList<>();

            for (int i = 2; i < Objects.requireNonNull(sheet).getPhysicalNumberOfRows(); i = i + 9) {
                //获取每一行数据
                row = sheet.getRow(i);
                if(row.getCell(0)==null && row.getCell(1)==null && row.getCell(2)==null && row.getCell(3)==null
                        && row.getCell(4)==null && row.getCell(5)==null && row.getCell(6)==null
                        && row.getCell(7)==null && row.getCell(8)==null){
                    registerResult.add(false);
                    continue;
                }
                SjqWeightProductionFaeces sjqWeightProductionFaeces = new SjqWeightProductionFaeces();
                if(row.getCell(0)!=null){
                    Cell cell = row.getCell(0);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setDate(cell.getStringCellValue());
                }
                if(row.getCell(1)!=null){
                    Cell cell = row.getCell(1);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setStartTime(cell.getStringCellValue());
                }
                if(row.getCell(2)!=null){
                    Cell cell = row.getCell(2);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setCloseTime(cell.getStringCellValue());
                }
                if(row.getCell(3)!=null){
                    Cell cell = row.getCell(3);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setSoftNormal(cell.getStringCellValue());
                }
                if(row.getCell(4)!=null){
                    Cell cell = row.getCell(4);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setMonitorNormal(cell.getStringCellValue());
                }
                if(row.getCell(5)!=null){
                    Cell cell = row.getCell(5);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setDataNormal(cell.getStringCellValue());
                }
                if(row.getCell(6)!=null){
                    Cell cell = row.getCell(6);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setConnectNormal(cell.getStringCellValue());
                }
                if(row.getCell(7)!=null){
                    Cell cell = row.getCell(7);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setOperator(cell.getStringCellValue());
                }
                if(row.getCell(8)!=null){
                    Cell cell = row.getCell(8);
                    cell.setCellType(CellType.STRING);
                    sjqWeightProductionFaeces.setNote(cell.getStringCellValue());
                }

                //海淀环卫
                Row row1 = sheet.getRow(i + 2);
                if(row1.getCell(4)==null && row1.getCell(5)==null){
                    registerResult.add(false);
                    continue;
                }
                if(row1.getCell(4)!=null){
                    sjqWeightProductionFaeces.setTrain1((int)row1.getCell(4).getNumericCellValue());
                }
                if(row1.getCell(5)!=null){
                    sjqWeightProductionFaeces.setWeight1(row1.getCell(5).getNumericCellValue());
                }

                //四清分公司
                Row row2 = sheet.getRow(i + 3);
                if(row2.getCell(4) == null && row2.getCell(5) == null){
                    registerResult.add(false);
                    continue;
                }
                if(row2.getCell(4) != null){
                    sjqWeightProductionFaeces.setTrain2((int)row2.getCell(4).getNumericCellValue());
                }
                if(row2.getCell(5) != null){
                    sjqWeightProductionFaeces.setWeight2(row2.getCell(5).getNumericCellValue());
                }

                //宝山
                Row row3 = sheet.getRow(i + 4);
                if(row3.getCell(4) == null && row3.getCell(5)==null){
                    registerResult.add(false);
                    continue;
                }
                if(row3.getCell(4)!=null){
                    sjqWeightProductionFaeces.setTrain3((int)row3.getCell(4).getNumericCellValue());
                }
                if(row3.getCell(5)!=null){
                    sjqWeightProductionFaeces.setWeight3(row3.getCell(5).getNumericCellValue());
                }

                //田丰
                Row row4 = sheet.getRow(i + 5);
                if(row4.getCell(4) == null && row4.getCell(5)==null){
                    registerResult.add(false);
                    continue;
                }
                if(row4.getCell(4)!=null){
                    sjqWeightProductionFaeces.setTrain4((int)row4.getCell(4).getNumericCellValue());
                }
                if(row4.getCell(5)!=null){
                    sjqWeightProductionFaeces.setWeight4(row4.getCell(5).getNumericCellValue());
                }

                //合计
                Row row5 = sheet.getRow(i + 6);
                if(row5.getCell(4) == null && row5.getCell(5)==null){
                    registerResult.add(false);
                    continue;
                }
                if(row5.getCell(4)!=null){
                    sjqWeightProductionFaeces.setTrainTotal((int)row5.getCell(4).getNumericCellValue());
                }
                if(row5.getCell(5)!=null){
                    sjqWeightProductionFaeces.setWeightTotal(row5.getCell(5).getNumericCellValue());
                }

                //合计
                Row row6 = sheet.getRow(i + 7);
                if(row6.getCell(4) == null && row6.getCell(5)==null){
                    registerResult.add(false);
                    continue;
                }
                if(row6.getCell(4)!=null){
                    sjqWeightProductionFaeces.setTrainResidue((int)row6.getCell(4).getNumericCellValue());
                }
                if(row6.getCell(5)!=null){
                    sjqWeightProductionFaeces.setWeightResidue(row6.getCell(5).getNumericCellValue());
                }
                sjqWeightProductionFaeces1.add(sjqWeightProductionFaeces);
            }
            for (SjqWeightProductionFaeces u : sjqWeightProductionFaeces1) {
                Boolean test = false;
                registerResult.add(test = sjqWeightProductionService.addFromFaecesExcel(u.getDate(), u.getStartTime()
                        , u.getCloseTime(), u.getSoftNormal(), u.getMonitorNormal(), u.getDataNormal(), u.getConnectNormal()
                        , u.getTrain1(), u.getWeight1(), u.getTrain2(), u.getWeight2(), u.getTrainTotal()
                        , u.getWeightTotal(),u.getTrain3(), u.getWeight3(), u.getTrain4(), u.getWeight4(), u.getTrainResidue(),u.getWeightResidue(), u.getOperator(), u.getNote()));
                System.out.println(test);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult(registerResult);
    }
}
